﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverChart.Configuration;
using System.Collections.Generic;
using SilverChart.Components;
using SilverChart.Core.Resources;
using System.Windows.Markup;
using SilverChart.Util;

namespace SilverChart.PieChart {
    public class PieChartModel : ChartBase, RelativeClientConsumer {
        private List<Path> Sectors;
        private Double valueSum;

        public PieChartModel(Element data)
            : base(data) {
            CreateDependences();

            valueSum = 0.0;
            Sectors = new List<Path>();

            CreatePieWidgets();
            //Background = BrushHelper.GetSolidColorBrush("Blue");
        }

        private void CreatePieWidgets() {
            String sectorDef = ResourcesHelper.Instance["PieChart_Sector"];
            foreach (Double value in Data.Values) {
                Path sectorPath = (Path)XamlReader.Load(sectorDef);
                valueSum += value;

                Sectors.Add(sectorPath);
                Children.Add(sectorPath);
            }
        }

        public PolarCoordinateAxis Axis { get; private set; }

        protected void CreateDependences() {
            IList<PolarCoordinateAxis> composites = WidgetsManager.GetCompatibleComponents<PolarCoordinateAxis>();
            if (composites == null || composites.Count <= 0) {
                IList<ChartComposite> chartCanvas = WidgetsManager.GetCompatibleComponents<ChartComposite>();
                if (chartCanvas == null || chartCanvas.Count <= 0) {
                    throw new InvalidOperationException("Not found ChartCompoiste component!");
                }
                Axis = new PolarCoordinateAxis();
                chartCanvas[0].Children.Add(Axis);
            }
            else {
                Axis = composites[0];
            }
        }

        public override string WidgetName {
            get {
                return "Pie Chart Model";
            }
        }

        public override List<LegendProperties> Legends {
            get {
                return null;
            }
        }

        protected override Size MeasureOverride(Size availableSize) {
            Size aSize = new Size(Math.Min(availableSize.Width, 10000), Math.Min(availableSize.Height, 10000));
            return aSize;
        }

        protected override Size ArrangeOverride(Size finalSize) {
            Double radius = Math.Min(finalSize.Width, finalSize.Height) / 2 * 0.8;
            Double currentAngle = 0.0;
            for (Int32 i = 0; i < Data.Values.Count; i++) {
                Path sectorPath = Sectors[i];
                Double StartX = Axis.OriginX;
                Double StartY = Axis.OriginY;

                Double deltaAngle = currentAngle + (Data.Values[i] / valueSum * 2 * Math.PI) / 2;
                Double deltaX = Axis.MapValueToCanvasLeft(3, deltaAngle) - Axis.OriginX;
                Double deltaY = Axis.MapValueToCanvasTop(3, deltaAngle) - Axis.OriginY;

                PathGeometry pathGeometry = (PathGeometry)sectorPath.FindName("pathGeometry");
                pathGeometry.Figures.Clear();
                PathFigure sectorFigure = new PathFigure();
                sectorFigure.StartPoint = new Point(StartX + deltaX, StartY + deltaY);
                pathGeometry.Figures.Add(sectorFigure);

                Double arcFirstPointX = Axis.MapValueToCanvasLeft(radius, currentAngle);
                Double arcFirstPointY = Axis.MapValueToCanvasTop(radius, currentAngle);
                LineSegment firstLine = new LineSegment();
                firstLine.Point = new Point(arcFirstPointX + deltaX, arcFirstPointY + deltaY);
                sectorFigure.Segments.Add(firstLine);

                currentAngle = currentAngle + Data.Values[i] / valueSum * 2 * Math.PI;
                Double arcSecondPointX = Axis.MapValueToCanvasLeft(radius, currentAngle);
                Double arcSecondPointY = Axis.MapValueToCanvasTop(radius, currentAngle);
                ArcSegment arc = new ArcSegment();
                arc.Point = new Point(arcSecondPointX + deltaX, arcSecondPointY + deltaY);
                arc.Size = new Size(radius, radius);
                arc.SweepDirection = SweepDirection.Clockwise;
                if (Data.Values[i] / valueSum > 0.5) {
                    arc.IsLargeArc = true;
                }
                sectorFigure.Segments.Add(arc);

                LineSegment secondLine = new LineSegment();
                secondLine.Point = new Point(StartX + deltaX, StartY + deltaY);
                sectorFigure.Segments.Add(secondLine);
            }
            return finalSize;
        }

        #region RelativeClientConsumer Members

        public RelativeClientProvider GetClientProvider() {
            IList<PolarCoordinateAxis> coordinates =
                WidgetsManager.GetCompatibleComponents<PolarCoordinateAxis>();
            if (coordinates != null && coordinates.Count > 0) {
                return coordinates[0];
            }
            throw new InvalidOperationException("CartesianCoordinateComposite not found!");
        }

        #endregion
    }
}
